from core.tools.encryption import hashlib_md5
from finance_c2c.local_settings import ONLINE


class CzbApiBase(object):
    @classmethod
    def get_source(cls, parameters):
        """
        :param parameters:
        :return:
        """
        source = ""
        base_sorted = sorted(parameters)
        for i in base_sorted:
            source = source + str(i) + str(parameters[i])
        return source

    def get_sign(self, parameters, app_secret):
        """
        获取签名
        :param parameters:
        :param app_secret:
        :return:
        """
        source = self.get_source(parameters)
        content = app_secret + source + app_secret
        sign = hashlib_md5(content)
        return sign

    def get_token_key(self, key):
        """
        从缓存中获取 access_token
        :return:
        """
        if ONLINE:
            return "czb_%s" % key
        else:
            return "czb_cs_%s" % key

